package it.canraid.leaflet.control;

import it.canraid.leaflet.R;
import it.canraid.leaflet.control.activity.DrawActivity;
import it.canraid.leaflet.view.FlingPanel;
import android.graphics.Color;
import android.view.View;
import android.view.View.OnClickListener;

public class ClickButtonListener implements OnClickListener {
    private int[] idImage;
    private DrawActivity context;

    public ClickButtonListener(DrawActivity context, View v, int[] listResource, int firstValue) {
	super();
	this.context = context;
	if (listResource != null && listResource.length > 0)
	    this.idImage = listResource;
	else
	    this.idImage = new int[] { 0 };

	v.setOnClickListener(this);
	Kernel.R().setClickStatus(v, firstValue);
	v.setBackgroundResource(idImage[firstValue % idImage.length]);
	v.setEnabled(true);
    }

    public void onClick(View v) {
	switch (v.getId()) {
	case R.id.leftMenuImage: // SOLO R/W mode
	case R.id.rightMenuImage: // SOLO R/W mode
	    FlingPanel bodyPanelEditable = (FlingPanel) context.bodyPanel;
	    context.pocketPanel.collapse();
	    // erase-restore
	    int erase = Kernel.R().getActiveEraseRestore(Kernel.ID_TEXT_VIEW, R.id.eraseImage);
	    int restore = Kernel.R().getActiveEraseRestore(Kernel.ID_TEXT_VIEW, R.id.restoreImage);
	    if (restore > 0) {
		context.restoreButton.setEnabled(true);
		context.restoreButton.setAlpha(1f);
	    } else {
		context.restoreButton.setEnabled(false);
		context.restoreButton.setAlpha(Kernel.ALPHA_DISABLE_VIEW);
	    }
	    if (erase > 0) {
		context.eraseButton.setEnabled(true);
		context.eraseButton.setAlpha(1f);
	    } else {
		context.eraseButton.setEnabled(false);
		context.eraseButton.setAlpha(Kernel.ALPHA_DISABLE_VIEW);
	    }
	    // This
	    if (v.getId() == R.id.rightMenuImage) {
		bodyPanelEditable.getSecondView();
		// context.drawBody.invalidate();
		context.leftButton.setVisibility(View.VISIBLE);
		context.rightButton.setVisibility(View.INVISIBLE);
	    } else if (v.getId() == R.id.leftMenuImage) {
		bodyPanelEditable.getFirstView();
		// context.textBody.invalidate();
		context.leftButton.setVisibility(View.INVISIBLE);
		context.rightButton.setVisibility(View.VISIBLE);
	    }
	    break;

	case R.id.sImage:
	    // Other
	    context.dimensionButton[1].setBackgroundResource(Kernel.M_IMAGE[0]);
	    Kernel.R().setClickStatus(context.dimensionButton[1], 0);
	    context.dimensionButton[2].setBackgroundResource(Kernel.L_IMAGE[0]);
	    Kernel.R().setClickStatus(context.dimensionButton[2], 0);

	    // This
	    Kernel.R().setClickStatus(v, 1);
	    v.setBackgroundResource(idImage[1]);
	    Kernel.R().sizeCurrentDraw = Kernel.SIZE_DRAW[0];
	    Kernel.R().sizeCurrentText = Kernel.SIZE_TEXT[0];

	    break;
	case R.id.mImage:
	    // Other
	    context.dimensionButton[0].setBackgroundResource(Kernel.S_IMAGE[0]);
	    Kernel.R().setClickStatus(context.dimensionButton[0], 0);
	    context.dimensionButton[2].setBackgroundResource(Kernel.L_IMAGE[0]);
	    Kernel.R().setClickStatus(context.dimensionButton[2], 0);

	    // This
	    Kernel.R().setClickStatus(v, 1);
	    v.setBackgroundResource(idImage[1]);
	    Kernel.R().sizeCurrentDraw = Kernel.SIZE_DRAW[1];
	    Kernel.R().sizeCurrentText = Kernel.SIZE_TEXT[1];
	    break;
	case R.id.lImage:
	    // Other
	    context.dimensionButton[1].setBackgroundResource(Kernel.M_IMAGE[0]);
	    Kernel.R().setClickStatus(context.dimensionButton[1], 0);
	    context.dimensionButton[0].setBackgroundResource(Kernel.S_IMAGE[0]);
	    Kernel.R().setClickStatus(context.dimensionButton[0], 0);

	    // This
	    Kernel.R().setClickStatus(v, 1);
	    v.setBackgroundResource(idImage[1]);
	    Kernel.R().sizeCurrentDraw = Kernel.SIZE_DRAW[2];
	    Kernel.R().sizeCurrentText = Kernel.SIZE_TEXT[2];
	    break;
	case R.id.redImage:
	    // Other
	    context.colorButton[1].setBackgroundResource(Kernel.BLACK_IMAGE[0]);
	    Kernel.R().setClickStatus(context.colorButton[1], 0);
	    context.colorButton[2].setBackgroundResource(Kernel.BLUE_IMAGE[0]);
	    Kernel.R().setClickStatus(context.colorButton[2], 0);

	    // This
	    Kernel.R().setClickStatus(v, 1);
	    v.setBackgroundResource(idImage[1]);
	    Kernel.R().colorCurrent = Color.RED;
	    break;
	case R.id.blackImage:
	    // Other
	    context.colorButton[0].setBackgroundResource(Kernel.RED_IMAGE[0]);
	    Kernel.R().setClickStatus(context.colorButton[0], 0);
	    context.colorButton[2].setBackgroundResource(Kernel.BLUE_IMAGE[0]);
	    Kernel.R().setClickStatus(context.colorButton[2], 0);

	    // This
	    Kernel.R().setClickStatus(v, 1);
	    v.setBackgroundResource(idImage[1]);
	    Kernel.R().colorCurrent = Color.BLACK;
	    break;
	case R.id.blueImage:
	    // Other
	    context.colorButton[0].setBackgroundResource(Kernel.RED_IMAGE[0]);
	    Kernel.R().setClickStatus(context.colorButton[0], 0);
	    context.colorButton[1].setBackgroundResource(Kernel.BLACK_IMAGE[0]);
	    Kernel.R().setClickStatus(context.colorButton[1], 0);

	    // This
	    Kernel.R().setClickStatus(v, 1);
	    v.setBackgroundResource(idImage[1]);
	    Kernel.R().colorCurrent = Color.BLUE;
	    break;
	case R.id.font1Image:
	    // Other
	    context.fontButton[1].setBackgroundResource(Kernel.FONT_2_IMAGE[0]);
	    Kernel.R().setClickStatus(context.fontButton[1], 0);
	    context.fontButton[2].setBackgroundResource(Kernel.FONT_3_IMAGE[0]);
	    Kernel.R().setClickStatus(context.fontButton[2], 0);

	    // This
	    Kernel.R().setClickStatus(v, 1);
	    v.setBackgroundResource(idImage[1]);
	    Kernel.R().fontCurrent = Kernel.FONT[0];
	    break;
	case R.id.font2Image:
	    // Other
	    context.fontButton[0].setBackgroundResource(Kernel.FONT_1_IMAGE[0]);
	    Kernel.R().setClickStatus(context.fontButton[0], 0);
	    context.fontButton[2].setBackgroundResource(Kernel.FONT_3_IMAGE[0]);
	    Kernel.R().setClickStatus(context.fontButton[2], 0);

	    // This
	    Kernel.R().setClickStatus(v, 1);
	    v.setBackgroundResource(idImage[1]);
	    Kernel.R().fontCurrent = Kernel.FONT[1];
	    break;
	case R.id.font3Image:
	    // Other
	    context.fontButton[0].setBackgroundResource(Kernel.FONT_1_IMAGE[0]);
	    Kernel.R().setClickStatus(context.fontButton[0], 0);
	    context.fontButton[1].setBackgroundResource(Kernel.FONT_2_IMAGE[0]);
	    Kernel.R().setClickStatus(context.fontButton[1], 0);

	    // This
	    Kernel.R().setClickStatus(v, 1);
	    v.setBackgroundResource(idImage[1]);
	    Kernel.R().fontCurrent = Kernel.FONT[2];
	    break;
	}

    }

}
